﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Step4.Unit2.Models;
using Step4.Unit2.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading.Tasks;

namespace Step4.Unit2.Controllers
{
    /// <summary>
    /// 测试通过注入IHttpContextAccessor来获取请求上下文
    /// </summary>
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IStudentService _studentService;

        // 构造函数，依赖注入
        public HomeController(ILogger<HomeController> logger,IStudentService studentService)
        {
            _logger = logger;
            _studentService = studentService;
        }

        public IActionResult Index()
        {

            // HttpContext.Request;
            // Request;
            /*
               上面两者指向的是同一对象,因为在Controller(控制器)中，HttpContext.Request 用的比较多
               所以，在Controller中声明了Request属性，并把当前请求上下文中而Request对象赋值给了Request属性，方便使用
            */

            // HttpContext.Response;
            // Response;
            // (同上)

            _studentService.Save();
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
